package RoleService

import (
	"context"
	"database/sql"
	"seawolf-admin-go/internal/request/BaseReq"
	"seawolf-admin-go/utility/db"
	"seawolf-admin-go/utility/response"
)

func Info(ctx context.Context, req *BaseReq.I) *response.JsonResponse {
	obj := db.GetUnSafaTable(ctx, req.TableName).Handler(db.SoftDeleteHandler).WherePri(
		"id",
		req.Id,
	)
	// 过滤字段
	if len(req.InfoIgnoreProperty) > 0 {
		obj.Fields(obj.GetFieldsExStr(req.InfoIgnoreProperty))
	}
	res, err := obj.One()
	if err != nil {
		if err != sql.ErrNoRows {
			// 非 结果为空
			return response.FailByRequestMessage(nil, err.Error())
		}
	}
	result := res.Map()
	resCon, err := db.GetUnSafaTable(ctx, "sys_role_menu").Where(
		"role_id",
		result["id"],
	).Fields("menu_id").Array()
	result["menuIdList"] = resCon
	resCon, err = db.GetUnSafaTable(ctx, "sys_role_department").Where(
		"role_id",
		result["id"],
	).Fields("department_id").Array()
	result["departmentIdList"] = resCon
	return response.SuccessByRequestMessageData(nil, "获取成功",
		result)
}
